package com.situ.cloud.gateway.filter;

import com.situ.cloud.common.util.JWTUtil;
import com.situ.cloud.common.util.StringUtil;
import com.situ.cloud.gateway.feign.UserFeign;
import com.situ.cloud.gateway.security.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationTokenFilter implements WebFilter {
    @Autowired
    private UserFeign userFeign;



//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String token = request.getHeader(JWTUtil.HEADER_TOKEN_KEY);
//        if (StringUtil.isNotBlank(token)) {
//            Integer id = JWTUtil.getUserId(token);
//            if (id != null) {
//                LoginUser user = userFeign.getById(id);
//                SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user,user.getPassword(),user.getAuthorities()));
//            }
//        }
//        filterChain.doFilter(request, response);
//    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
       // exchange.getRequest()
        return null;
    }
}
